package com.ruoyi.manage.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.manage.domain.SysMechanism;
import com.ruoyi.manage.service.MechanismManageService;
import com.ruoyi.manage.threadLocal.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 满杯百香果🍎
 */
@RestController
@RequestMapping("/mechanism")  // manage/mechanism/list
public class MechanismManageController extends BaseController {

    @Autowired
    private MechanismManageService mechanismManageService;

    @RequestMapping(value = "/hello")
    public String hello(HttpServletRequest request){
        String username1 = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();
        String username = UserThreadLocal.getUsername();
        return "Hello,满杯百香果🍎 " + username;
    }

    @GetMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<SysMechanism> list = mechanismManageService.getList();
        return getDataTable(list);
    }


    @GetMapping("/getChildrenByParentId/{id}")
    public TableDataInfo getChildrenByParentId(@PathVariable Long id){
        startPage();
        List<SysMechanism> list = mechanismManageService.getChildrenByParentId(id);
        return getDataTable(list);
    }

    @GetMapping("/getListByMechanismCodeAndMechanismName")
    public TableDataInfo getListByMechanismCodeAndMechanismName(SysMechanism sysMechanism){
        startPage();
        List<SysMechanism> list = mechanismManageService.getListByMechanismCodeAndMechanismName(sysMechanism);
        return getDataTable(list);
    }

    @PostMapping("/addMechanism")
    public AjaxResult addMechanism(@RequestBody SysMechanism sysMechanism){
        return mechanismManageService.addMechanism(sysMechanism);
    }

    @DeleteMapping("/delMechanism/{id}")
    public AjaxResult delMechanism(@PathVariable("id") Long id){
        if (mechanismManageService.delMechanism(id) == 0){
            return AjaxResult.error("此机构有关联的子机构，删除失败！");
        }
        return AjaxResult.success("删除成功！");
    }


}
